// packageA/pages/operate/demonstration/demonstration.js
const App = getApp()
// let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
let reg = /^1[0-9]{10}$/;
var reg_em = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //邮箱正则表达式
import Toast from '../../../../weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: ['申请演示', '开户申请', '申请结佣'],
    follow_form: {
      unit_name: '',
      taxpayer_id_code: '',
      mobile: '',
      receive_mail: ''
    },
    currentIndex: 0,
    show: false,
    actions: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  onShow: function (options) {
    this.getRechargeList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  handleSelect() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    this.setData({
      ...this.data.follow_form,
      'follow_form.option_str': event.detail.name,
      'follow_form.recharge_id': event.detail.recharge_id,
    })
    console.log(event.detail);
  },
  inputOnChange(e) {
    console.log(e)
    let type = e.currentTarget.dataset.type
    let value = e.detail
    this.setData({
      follow_form: {
        ...this.data.follow_form,
        [type]: value
      }
    })
  },
  handleSaveForm() {
    console.log(this.data.follow_form)
    let _this = this
    let { mobile,receive_mail } = _this.data.follow_form
    if (mobile == '') {
      Toast.fail('手机号不能为空')
      return false
    }
    if (!reg.test(mobile)) {
      Toast.fail('手机号不符合要求');
      return false;
    }
    if (!reg_em.test(receive_mail)) {
      Toast.fail('邮箱格式有误');
      return false;
    }
    App._post('Business/addInvoice', _this.data.follow_form
      , function (result) {
        if (result.code === 0) {
          Toast({
            type: 'success',
            message: '提交成功',
            onClose: () => {
              wx.redirectTo({
                url: '../../operate/invoice/invoice-record',
              })
            },
          });
        } else {
          Toast.fail(result.msg)
        }
      })
  },
  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getRechargeList() {
    let _this = this
    App._post_form('Business/getRechargeSelectList', {
    }, function (result) {
      let arr = []
      result.data.list.forEach(item => {
        let obj = {
          name: item.option_str,
          recharge_id: item.recharge_id
        }
        arr.push(obj)
      });
      _this.setData({
        actions: arr
      })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})